//This file contains home-brewed Javascript code for Course Critique
//Created: 6/6/2010 - rjs


function initProfessorSearchForm() {
	xmlhttp.onreadystatechange=buildProfessorSearchForm;
	xmlhttp.open("GET",'http://sga.gatech.edu/c_staging/getProfessor.php',true);
	xmlhttp.send(null);
}

function buildProfessorSearchForm() {
	var divSearchForm = document.getElementById('divSearchForm'); 
	var	output="<p>Ok! Select a <strong>Professor</strong>:<br /><select name='Professor'>";
	var professors;
	if ((xmlhttp.readyState==4)) {	
		 	professors = JSON.parse(xmlhttp.responseText);
			for (var i in professors) {
		 		professor = professors[i];
			 	if (professor.ID != null) {
				 		output+="<option>"+professor.LastName+", "+professor.FirstName+"</option>";
				 }
		 	}
		 	
	}
	output+="</select><input type='submit' value='Search!' /></p>";
	divSearchForm.innerHTML=output;
	
}

function initCourseSearchForm() {
	xmlhttp.onreadystatechange=buildDepartmentSelectForm;
	xmlhttp.open("GET",'http://sga.gatech.edu/c_staging/getDepartment.php',true);
	xmlhttp.send(null);
}
function buildDepartmentSelectForm() {
	var divSearchForm=document.getElementById('divSearchForm');
	var output="<p>Ok! First, select a <strong>Department</strong>:<br /><select name='Department' onchange='initCourseSelect(this.value);'><option></option>";
	var departments;
	if ((xmlhttp.readyState==4)) {
		departments = JSON.parse(xmlhttp.responseText);
		for (var i in departments) {
			department=departments[i];
			if (department.ID != null) {
					output += "<option>"+department.Code+"</option>";
			}
		}
	}
	output+="</select><div id='divCourseForm'></div>";
	divSearchForm.innerHTML=output;
}

function initCourseSelect(deptCode) {
	xmlhttp.onreadystatechange=buildCourseSelectForm;
	xmlhttp.open("GET",'http://sga.gatech.edu/c_staging/getCourse.php?Department='+deptCode,true);
	xmlhttp.send(null);
}

function buildCourseSelectForm() {
	var divCourseForm=document.getElementById('divCourseForm');
	var output="<p>Finally, choose a <strong>Course Number</strong>:<br /><select name='CourseNumber'>";
	var courses;
	if ((xmlhttp.readyState==4)) {
		courses = JSON.parse(xmlhttp.responseText);
		for (var i in courses) {
			course=courses[i];
			if (course.ID != null) {
					output += "<option>"+course.CourseNumber+"</option>";
			}
		}
	}
	output+="</select><br /><input type='submit' value='Search!' /></p>";
	divCourseForm.innerHTML=output;
}
